package com.imooc.activiti.interceptor;

import org.activiti.engine.debug.ExecutionTreeUtil;
import org.activiti.engine.impl.agenda.AbstractOperation;
import org.activiti.engine.impl.interceptor.DebugCommandInvoker;
import org.activiti.engine.logging.LogMDC;

/**
 * @author lynn
 * @date 18-12-23 下午3:58
 */
public class MdcCommandInvoker extends DebugCommandInvoker {


    @Override
    public void executeOperation(Runnable runnable) {

        boolean mdcEnabled = LogMDC.isMDCEnabled();

        LogMDC.setMDCEnabled(true);

        if (runnable instanceof AbstractOperation) {
            AbstractOperation operation = (AbstractOperation)runnable;
            if (operation.getExecution() != null) {
                LogMDC.putMDCExecution(operation.getExecution());
            }
        }

        super.executeOperation(runnable);
        LogMDC.clear();

        if(!mdcEnabled){
            LogMDC.setMDCEnabled(false);
        }
    }

}
